package com.symbio.ishenapi.background.kit;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.symbio.ishenapi.common.kit.MessageKit;

import java.util.Map;

/**
 * @Author: chen
 * @Description:极光推送信息
 * @Date: Created in 10:34 2018/10/25
 * @Modified By:
 */
public class JdPush {

    private static String appKeyP;
    private static String masterSecretP;
    private static String appKeyD;
    private static String masterSecretD;

    // 设置好账号的app_key和masterSecret
    static {
        appKeyP = MessageKit.getKey(90001);
        masterSecretP = MessageKit.getKey(90002);
        appKeyD = MessageKit.getKey(90003);
        masterSecretD = MessageKit.getKey(90004);
    }

    //极光推送>>Android
    public static void jpushAndroid(Map<String, String> parm) {
        //创建JPushClient(极光推送的实例)
        JPushClient jpushClient = getJpushClient(parm.get("client"));
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.all())//你项目中的所有用户
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //这里是指定开发环境,不用设置也没关系
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    //极光推送>>ios
    public static  void jpushIOS(Map<String, String> parm) {
        //创建JPushClient(极光推送的实例)
        JPushClient jpushClient = getJpushClient(parm.get("client"));
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())//ios平台的用户
                .setAudience(Audience.all())//所有用户
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(parm.get("msg"))
                                .setBadge(+1)
                                .setSound("happy")//这里是设置提示音(更多可以去官网看看)
                                .addExtras(parm)
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    //获取JPushClient对象爱

    public static JPushClient getJpushClient(String clientTag){
        JPushClient jpushClient = null;
        switch (clientTag){
            case "1":
                jpushClient = new JPushClient(masterSecretD,appKeyD);
                break;
            case "2":
                jpushClient = new JPushClient(masterSecretP,appKeyP);
                break;
        }

        return jpushClient;
    }
}
